const {default: Axios} = require("axios");
const QQBot = require("./QQBot");

class MyBot extends QQBot {
	/**
	 * @param {cq.GroupMsg} msg
	 */
	group(msg) {
		if (msg.group_id == "1044248231") {
			// 优惠券
		} else if (msg.group_id == "922317791") {
			// 免单
			this.parseFree(msg.message, msg.time * 1e3);
		} else {
			console.log(Date.now() / 1000 - msg.time);
			console.log(msg.group_id, msg.message);
		}
	}
	/**
	 *
	 * @param {cq.FriendMsg} msg
	 */
	friend(msg) {
		if (msg.user_id == "929909260") {
			this.parseFree(msg.message, msg.time * 1e3);
		}
	}
	/**
	 * @param {string} text
	 * @param {number} [time]
	 */
	parseFree(text, time) {
		time = time || Date.now();
		let lines = text.split("\n");
		let body = {};
		let m;
		for (let line of lines) {
			if ((m = /\d+(\.\d+)?元/.exec(line))) {
				body.price = parseFloat(m[0]);
			} else if ((m = /[\(¥](\w+)[\)¥]/.exec(line))) {
				body.word = `¥${m[1]}¥`;
			} else if (!/抢到免单请回复|Tao寶搜索/.test(line) && !body.title) body.title = line.trim();
		}
		if (body.word && body.title) {
			body.time = time;
			console.log(new Date(time).toLocaleString(), body.title);
			Axios.post("https://quan2go.inu1255.cn/api/material/addfree", body);
			// Axios.post("http://localhost:3000/api/material/addfree", body);
			return true;
		}
		console.error(text);
	}
}

let bot = new MyBot(process.argv[2]);
